package net.wanho.shop.service.impl;

import net.wanho.shop.po.GoodsCats;
import net.wanho.shop.mapper.GoodsCatsMapper;
import net.wanho.shop.service.GoodsCatsService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class GoodsCatsServiceImpl implements GoodsCatsService {

    private final GoodsCatsMapper goodsCatsMapper;

    /**
     * 商品分类表分页查询
     * @param pageNum
     * @param pageSize
     * @param goodsCats
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, GoodsCats goodsCats) {
        if(ObjectUtil.isNull(goodsCats)){
            goodsCats = new GoodsCats();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsCats> goodsCatsList = goodsCatsMapper.select(goodsCats);
        return new PageInfo(goodsCatsList);
    }

    /**
     * 根据主键查询商品分类表
     * @param catId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public GoodsCats findById(Serializable catId) {
        return goodsCatsMapper.selectById(catId);
    }

    /**
     * 商品分类表新增
     * @param goodsCats
     */
    @Override
    public void add(GoodsCats goodsCats) {
        goodsCatsMapper.insert(goodsCats);
    }

    /**
     * 修改商品分类表
     * @param goodsCats
     */
    @Override
    public void modify(GoodsCats goodsCats) {
        goodsCatsMapper.update(goodsCats);
    }

    /**
     * 根据主键删除商品分类表
     * @param catId
     */
    @Override
    public void removeById(Serializable catId) {
        if(((String)catId).contains(",")){
            goodsCatsMapper.deleteByIds(((String)catId).split(","));
        }else {
            goodsCatsMapper.deleteById(catId);
        }
    }
}
